/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.raflik.queryxml.ui;

import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author raflik
 */
class QueryHistory extends DefaultComboBoxModel implements IHistoryListModel {

    private int limit = 15;

    private void ensureLimit() {
        final int size = getSize();
        for (int i = size - 1; i >= limit; i--) {
            removeElementAt(i);
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
        ensureLimit();
    }

    public void store(Object value) {
        if (value == null) {
            return;
        }
        int index = getIndexOf(value);
        if (index > -1) {
            removeElementAt(index);
        }
        addElement(value);
        setSelectedItem(value);
        ensureLimit();
    }

    public void delete(Object value) {
        removeElement(value);
    }

    public void clear() {
        removeAllElements();
    }

    @Override
    public Object getElementAt(int index) {
        return super.getElementAt(getSize() - index - 1);
    }

}
